package com.kc.web.controller.business;

import com.google.common.collect.Lists;
import com.kc.business.domain.KcAppVersion;
import com.kc.business.service.KcAppVersionService;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.model.LoginUser;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.AppOsTypeEnum;
import com.kc.common.utils.ServletUtils;
import com.kc.common.utils.StringUtils;
import com.kc.framework.web.service.TokenService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author TP
 * @date 2021-01-30 18:23
 */
@RestController
@RequestMapping("/app/version")
public class KcAppVersionController extends BaseController {

    @Autowired
    private KcAppVersionService appVersionService;

    @Autowired
    private TokenService tokenService;

    @PreAuthorize("@ss.hasPermi('app:version:list')")
    @GetMapping("/list")
    public TableDataInfo getList(KcAppVersion appVersion) {
        startPage();
        List<KcAppVersion> list = appVersionService.getList(appVersion);
        return getDataTable(list);
    }

    @GetMapping("/latest")
    public TableDataInfo getList() {
        List<KcAppVersion> result = Lists.newArrayList();
        KcAppVersion iosVersion = appVersionService.getLatestVersion(AppOsTypeEnum.IOS.getValue());
        KcAppVersion androidVersion = appVersionService.getLatestVersion(AppOsTypeEnum.ANDROID.getValue());
        result.add(iosVersion);
        result.add(androidVersion);
        return getDataTable(result);
    }

    @PreAuthorize("@ss.hasPermi('app:version:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody KcAppVersion appVersion) {
        synchronized (this) {
            // 查重
            List<KcAppVersion> existAppVersions = appVersionService.getByVersionAndOsType(appVersion.getVersion(), appVersion.getOsType());
            if (CollectionUtils.isNotEmpty(existAppVersions)) {
                return AjaxResult.error("版本号重复，请重新指定");
            }
            if (AppOsTypeEnum.ANDROID.getValue().equals(appVersion.getOsType())) {
                if (StringUtils.isEmpty(appVersion.getPackageUrl())) {
                    return AjaxResult.error("请上传安装包");
                }
            }
            // 获取当前最新版本，判断当前最新
            KcAppVersion currentAppVersion = appVersionService.getLatestVersion(appVersion.getOsType());
            if(currentAppVersion != null){
                String currentVersion = currentAppVersion.getVersion();
                if (StringUtils.compare(appVersion.getVersion(), currentVersion) <= 0) {
                    return AjaxResult.error("已存在更高版本");
                }
            }
            // 新增
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            appVersion.setCreateTime(LocalDateTime.now());
            appVersion.setCreateBy(loginUser.getUsername());
            return toAjax(appVersionService.add(appVersion));
        }
    }

    @PreAuthorize("@ss.hasPermi('app:version:delete')")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return toAjax(appVersionService.delete(id));
    }
}
